import axios from "axios";
import store from "@/store/index.js"
const request_url = "https://wx.sphhn.com:82/api"
let request = axios.create({
	// #ifdef H5
	baseURL: '/api', // h5使用代理处理跨域
	// #endif
	// #ifdef APP-PLUS || MP-WEIXIN
	baseURL: request_url, // 直链请求
	// #endif
	headers: {
		"Content-Type": "application/json"
	},
	timeout: 120000
})
request.interceptors.request.use(
	config => {
		try {
			// uni.showNavigationBarLoading()
			const token = uni.getStorageSync('token');
			const userinfo = uni.getStorageSync('userinfo')
			if (config.needAuth === false) {
				config.headers.Authorization = ""
			} else if (token) {
				// 判断是否存在token，如果存在的话，则每个http header都加上token
				config.headers.Authorization = userinfo.USER_ID + '|' + token; //Authorization是登录接口返回
			}
			return config;

		} catch (e) {
			uni.hideNavigationBarLoading()
			uni.showToast({
				title: '发生错误，请稍后重试',
				position: 'center',
				icon: 'none',
				duration: 2000
			})
		}
	},
	err => {
		uni.hideNavigationBarLoading()
		return Promise.reject(err);
	}
);
// http response 拦截器
request.interceptors.response.use(
	response => {
		console.log(response)
		//拦截响应，做统一处理
		if (response.data.State != 1) {
			uni.showToast({
				title: response.data.Msg,
				icon: "none",
				duration: 2000
			})
			if (process.env.NODE_ENV === 'development') {
				// console.error(response.data.msg)
			}
		}
		if (response.data.State == 401) {
			uni.showToast({
				title: '登录已过期，请重新登录',
				icon: 'none',
				duration: 2000
			})
			store.commit("logout")
		}
		uni.hideNavigationBarLoading()
		return response.data;
	},
	//接口错误状态处理，也就是说无响应时的处理
	error => {
		console.log(error, 'error');
		uni.hideNavigationBarLoading()
		const {
			response: {
				status,
				errMsg: statusText,
				data: {
					message
				}
			}
		} = error;
		const token = uni.getStorageSync('token');
		if (status == 40005 && token) { // 登录过期处理
			uni.clearStorageSync()
			uni.showToast({
				title: '登录已过期，请重新登录',
				icon: 'none',
				duration: 2000
			})
		} else {
			uni.showToast({
				title: `请求错误，请稍后重试`,
				position: 'center',
				icon: 'none',
				duration: 2000
			})
			console.error(`请求错误${status||''}：${statusText||message||''}`)
		}
		return Promise.reject(error); // 返回接口返回的错误信息
	}
);
//真机获取
axios.defaults.adapter = function(config) {
	return new Promise((resolve, reject) => {
		var settle = require('axios/lib/core/settle');
		var buildURL = require('axios/lib/helpers/buildURL');
		uni.request({
			method: config.method.toUpperCase(),
			url: buildURL(config.baseURL + config.url, config.params, config.paramsSerializer),
			header: config.headers,
			data: config.data,
			dataType: config.dataType,
			responseType: config.responseType,
			sslVerify: config.sslVerify,
			complete: function complete(response) {
				response = {
					data: response.data,
					status: response.statusCode,
					errMsg: response.errMsg,
					header: response.header,
					config: config
				};

				settle(resolve, reject, response);
			}
		})
	})
}
export default request;
